package com.tensquare.gathering.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tensquare.gathering.dao.GatheringDao;
import com.tensquare.gathering.pojo.Gathering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import util.IdWorker;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class GatheringService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private GatheringDao gatheringDao;
    public void save(Gathering gathering) {
        gathering.setId(idWorker.nextId()+"");
        gatheringDao.insert(gathering);
    }

    public List<Gathering> findAll() {
        return gatheringDao.selectList(null);

    }
    @Cacheable(value = "gathering",key = "#gatheringId")
    public Gathering findById(String gatheringId) {
        return gatheringDao.selectById(gatheringId);
    }

    @CacheEvict(value = "gathering",key = "#gathering.id")
    public void updateByGatheringId(Gathering gathering) {
        gatheringDao.updateById(gathering);
    }
    @CacheEvict(value = "gathering",key = "#gatheringId")
    public void deleteByGatheringId(String gatheringId) {
        gatheringDao.deleteById(gatheringId);
    }

    public Page<Gathering> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<Gathering> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        Page<Gathering> page1=new Page<>(page,size);
        List<Gathering> gatherings = gatheringDao.selectPage(page1, wrapper);
        page1.setRecords(gatherings);
        return page1;
    }

    public List<Gathering> findBySearch(Map<String, Object> map) {
        return gatheringDao.selectByMap(map);
    }
    public Page findByCity(String city,Integer page,Integer size){
        Page page1=new Page(page,size);
        List<Gathering>list=gatheringDao.selectByCity(city,page1);
        page1.setRecords(list);
        return page1;

    }
}
